package com.zjw.algorithm.ready1;

public class _最大交换 {

    public static void main(String[] args) {
        System.out.println(new _最大交换().maximumSwap(98368));
    }


    public int maximumSwap(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int pl = 0;
        int pr = chars.length - 1;
        char maxChar = chars[pl];
        int indexMax = pl;


        while (pl < chars.length - 1) {
            while (pr > pl) {
                if (chars[pr] > maxChar) {
                    indexMax = pr;
                    maxChar = chars[pr];
                }
                pr--;
            }

            if (indexMax == pl) {
                pl++;
                pr = chars.length - 1;
                indexMax = pl;
                maxChar = chars[indexMax];
            }else{
               char temp = chars[pl];
               chars[pl] = chars[indexMax];
               chars[indexMax] = temp;
               break;
            }
        }


        return Integer.parseInt(String.valueOf(chars));
    }
}
